import { computed, ref, onUnmounted } from "vue";
import dayjs from "dayjs";
//封装倒计时逻辑函数
export const useCountDown = () => {
  let timer = null;
  //声明响应式的数据
  const time = ref(0);
  //格式化时间为xx分xx秒
  const formatTime = computed(() => dayjs.unix(time.value).format("mm分ss秒"));
  // 开启倒计时的函数
  const start = (currentTime) => {
    //开始倒计时的逻辑
    //核心逻辑的编写：每隔1s时间-1
    time.value = currentTime;
    timer = setInterval(() => {
      time.value--;
    }, 1000);
  };
  onUnmounted(() => {
    timer && clearInterval(timer);
  });
  return {
    start,
    formatTime,
  };
};
